﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow_Core_API.Persisted;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Service.Dto.Fundamentals;
using WorkflowCore.Service.Manger;
using WorkflowCore.Service.Persisted;
using WorkflowCore.Services;

namespace WorkflowCore.Service.Steps
{
    /// <summary>
    /// 审批步骤
    /// </summary>
    public class AuditStep : StepBody
    {
        private readonly WorkflowCoreDbcontext dbcontext;
        private readonly IWorkflowManger workflowManger;
        public readonly IWorkflowRegistry workflowRegistry;
        private readonly GetWorkflowCoreUserData getWorkflowCoreUserData;
        /// <summary>
        /// 操作人ID
        /// </summary>
        private long auditUserId = 0;
        /// <summary>
        /// 操作人姓名
        /// </summary>
        private string auditUserName;

        public AuditStep(WorkflowCoreDbcontext dbcontext, IWorkflowManger workflowManger, IWorkflowRegistry workflowRegistry, GetWorkflowCoreUserData getWorkflowCoreUserData)
        {
            this.dbcontext = dbcontext;
            this.workflowManger = workflowManger;
            this.workflowRegistry = workflowRegistry;
            this.getWorkflowCoreUserData = getWorkflowCoreUserData;
        }

        public override ExecutionResult Run(IStepExecutionContext context)
        {
            List<PersonNode> Users = getWorkflowCoreUserData.ShoWorkflowCoreUserData();
            auditUserId = Convert.ToInt64(context.Item);
            if (!context.ExecutionPointer.EventPublished)
            {
                if (auditUserId > 0)
                {
                    auditUserName = Users.First(p => p.UserId == auditUserId).UserName;
                }

                workflowManger.AddAudit(context, OperateType.Approval, WorkFlowAuditStatus.UnAudited
                    , null, auditUserId, auditUserName);

                Console.WriteLine($"等待{context.Step.Name} {auditUserName} 审批");


                // 等待审批
                return ExecutionResult.WaitForEvent(WorkflowConst.WaitForEventName
                    , Guid.NewGuid().ToString()
                    , DateTime.Now);
            }


            #region 激活子节点(审批，驳回)

            var eventData = (context.ExecutionPointer.EventData as WorkflowAuditInput)!;
            //如果持久化数据为null
            if (context.PersistenceData == null)
            {
                // 将WorkflowAuditInput事件数据持久化到所有子节点，
                // 此处list有n条就会按照子 节点*n 构建下一步节点
                var result = ExecutionResult.Branch(new List<object> { eventData }, new ControlPersistenceData { ChildrenActive = true });
                // GetDescription(): 是个枚举扩展方法，获取Desription特性的作用。
                result.OutcomeValue = eventData.AuditOperateType.GetDescription();
                return result;
            }

            //激活子节点
            if ((context.PersistenceData is ControlPersistenceData data) && (data.ChildrenActive))
            {
                if (context.Workflow.IsBranchComplete(context.ExecutionPointer.Id))
                    return ExecutionResult.Next();
                else
                {
                    var result = ExecutionResult.Persist(context.PersistenceData);
                    result.OutcomeValue = eventData.AuditOperateType.GetDescription();
                    return result;
                }
            }

            #endregion

            return ExecutionResult.Next();
        }
    }
}
